package com.cart.utils;

import com.cart.models.Admin;
import com.cart.models.User;

import java.sql.*;

public class AdminFileUtil {

    // MySQL 数据库的连接信息
    private static final String URL = "jdbc:mysql://localhost:3306/cartManager";
    private static final String USER = "root";
    private static final String PASSWORD = "xfn123";

    // 读取管理员信息
    public static Admin readAdminFromFile() {
        Admin admin = null;
        try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {
            String query = "SELECT * FROM admin WHERE id = 1"; // 假设我们只存储一个管理员信息，ID为1
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(query);

            if (rs.next()) {
                String username = rs.getString("username");
                String password = rs.getString("password");
                String email = rs.getString("email");
                String phone = rs.getString("phone");
                String registrationDate = rs.getString("registration_date");

                admin = new Admin(1, username, password, email, phone, registrationDate);
            } else {
                System.out.println("管理员信息不存在，创建默认管理员...");
                admin = new Admin(1, "admin", User.md5Hash("ynuinfo#777"), "admin@example.com", "123456789", "2024-01-01");
                writeAdminToFile(admin);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admin;
    }

    // 写入管理员信息到MySQL数据库
    public static void writeAdminToFile(Admin admin) {
        try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {
            // 检查管理员是否已存在
            String queryCheck = "SELECT COUNT(*) FROM admin WHERE id = 1";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(queryCheck);
            rs.next();
            int count = rs.getInt(1);

            if (count > 0) {
                // 如果管理员信息已存在，执行更新操作
                String updateQuery = "UPDATE admin SET username=?, password=?, email=?, phone=?, registration_date=? WHERE id=1";
                try (PreparedStatement pstmt = conn.prepareStatement(updateQuery)) {
                    pstmt.setString(1, admin.getUsername());
                    pstmt.setString(2, admin.getPassword());
                    pstmt.setString(3, admin.getEmail());
                    pstmt.setString(4, admin.getPhone());
                    pstmt.setString(5, admin.getRegistrationDate());
                    pstmt.executeUpdate();
                }
            } else {
                // 如果管理员信息不存在，执行插入操作
                String insertQuery = "INSERT INTO admin (id, username, password, email, phone, registration_date) VALUES (1, ?, ?, ?, ?, ?)";
                try (PreparedStatement pstmt = conn.prepareStatement(insertQuery)) {
                    pstmt.setString(1, admin.getUsername());
                    pstmt.setString(2, admin.getPassword());
                    pstmt.setString(3, admin.getEmail());
                    pstmt.setString(4, admin.getPhone());
                    pstmt.setString(5, admin.getRegistrationDate());
                    pstmt.executeUpdate();
                }
            }

            System.out.println("管理员信息已写入数据库！");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
